package br.com.rookgraph.branchreduce;

import br.com.rookgraph.Edge;
import br.com.rookgraph.Graph;
import br.com.rookgraph.heuristica.Heuristica;


/**
 * Depuração do BrancheReduce conforme a
 * Heurística, especificada.
 *
 * @author Luan Pontes
 * @since 1-11-12
 * @changes 22-11-12
 *
 */
public class BranchReduceDepuration extends BranchReduce {

	private int interecaoExt;
	private int interecaoInt;

	public BranchReduceDepuration(Graph original, Graph tree, Heuristica heuristica) {
		super(original, tree, heuristica);
	}

	@Override
	public Graph execute() {

		System.out.println("\n GRAFO ORIGINAL \n");
		getOriginal().showMatix();

		System.out.println("\n\n ARVORE INICIAL \n");
		getTree().showMatix();

		Graph treefinal = super.execute();
		System.out.println("\n ARVORE FINAL \n");
		treefinal.showMatix();

		return treefinal;
	}

	@Override
	public Edge[] createCuttingList(){
		interecaoInt = 0;
		Edge[] listCut = super.createCuttingList();
		//System.out.println("\n");
		System.out.println("\n\t Iteração externa > "+ (++interecaoExt));
		System.out.println("\t\t Lista de corte: ");
		showEdges(listCut, false);
		return listCut;
	}

	@Override
	public Edge selectCutEdge(final Edge[] listCut) {
		Edge cutEdge = super.selectCutEdge(listCut);
		System.out.println("\n\t\t\t Iteração interna > "+ (++interecaoInt));
		System.out.print("\t\t\t\t Aresta de Corte: ");
		System.out.println(""+cutEdge);
		return cutEdge;
	}

	@Override
	public Edge[] createReplacementListToCuttingEdge(final Edge arestaCut){
		Edge[] repList = super.createReplacementListToCuttingEdge(arestaCut);
		System.out.println("\t\t\t\t Candidatas a Rep.: ");
		showEdges(repList, true);
		return repList;
	}

	@Override
	public Edge selectEdgeFromReplamentList(final Edge[] listRep, Edge edgeCut) {
		Edge repEdge = super.selectEdgeFromReplamentList(listRep, edgeCut);
		System.out.print("\t\t\t\t Replace: ");
		System.out.println("   "+repEdge);
		return repEdge;
	}

	@Override
	public boolean isTreeBest(Graph graphBest) {
		boolean treeBest = super.isTreeBest(graphBest);
		// System.out.println("\t\t\t\tA Arvore atual possui menos braches que a melhor? " + treeBest);
		return treeBest;
	}

	public void showEdges(Edge[] result, boolean nivel2) {
		String ident = nivel2 ? "\t\t\t\t\t\t  " : "\t\t\t";
		for (Edge edge : result) {
			System.out.println(ident+edge);
		}
	}

}
